<?php
	/////Reading from database is fine but not yet fully functional with updating it
	include("initialize.php");
?>
<html>
<head>
	<title>GradeGrid - Manage Grading Rubrics</title>
	<script type="text/javascript" language="javascript">
		var templateIDs = new Array()
		
		function editTemplate(){
			var templatesAvailable = document.getElementsByName("template")
			var i = 0
			var limit = templatesAvailable.length
			
			while(i < limit){
				if(templatesAvailable[i].checked){
					var windowURL = "editTemplate.php?template_id=" + templateIDs[i]
					window.open(windowURL, "_blank", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0")
					return
				}
				
				i++;
			}
			
			alert("Please select a template.")
		}
	</script>
	<?php
		include("header.php");
		include("write_functions.php");
		connect("localhost", "root", "", "gradegriddatabase");
		$classid = mysql_real_escape_string($_GET["id"]);
		
		function insertTemplateId($templateid){
			echo "<script type='text/javascript' language='javascript'>\n";
			echo "templateIDs.push($templateid)\n";
			echo "</script>";
		}
		
		if(isset($_POST["template"])){
			$templateid = mysql_real_escape_string($_POST["template"]);
			mysql_query("UPDATE class SET template_id = '$templateid' WHERE class_id = '$classid'");
		}
	?>
</head>
<body onLoad="writeCopyright()">
	<div class="main">
		
		<?php include("boilerplate.php"); ?>
		<?php include("loginnavigation.php"); ?>
		
		<?php if($_SESSION["logged_in"]): ?>
			<div style="float: left;">
			<p>
			
			<?php
			$accountid_qs = sprintf("SELECT account_id FROM account WHERE username = '%s'",
			                        mysql_real_escape_string($_SESSION["username"]));
			$account_array = mysql_fetch_array(mysql_query($accountid_qs));
			$accountid = mysql_real_escape_string($account_array["account_id"]);
			
			//Display the templates. The template used for this class is selected.
			//Query for all the templates first...
			$query_template = "SELECT * FROM template WHERE account_id = '$accountid'";
			$result_template=mysql_query($query_template);
			$num = mysql_num_rows($result_template);
			
			//...and then query for the template used for this class.
			$query_currTemplate = sprintf("SELECT template_id, template_name FROM template WHERE account_id='%s'",
			                              $accountid);
			$result_currTemplate = mysql_query($query_currTemplate);
			
			$templateused_qs = "SELECT template_id FROM class WHERE class_id = '$classid'";
			$template_array = mysql_fetch_array(mysql_query($templateused_qs));
			$templateused_id = $template_array["template_id"];
			
			$classname_query = mysql_query("SELECT class_name, course_name FROM class, course WHERE class_id='$classid'
			                               AND class.course_id = course.course_id");
			$classname_array = mysql_fetch_array($classname_query);
			$classname = $classname_array["course_name"] . " - " .$classname_array["class_name"];
			?>
			
			<div class="form_section"><h1>Set/Change Grading Rubric: <?php echo $classname; ?></h1></div>
			<p>You currently have the following grading rubrics:</p>
			<?php 
			echo "<form method='post' action='manage_rubrics.php?id=$classid'>\n";
			$index=0;
			$currTemplate_name=mysql_result($result_currTemplate,$index, "template_name"); 
			
				while ($index<$num):
					$template_id=mysql_result($result_template,$index,"template_id"); 	
					$template_name = mysql_result($result_template, $index, "template_name");
					if ($templateused_id == $template_id) {
						//The template this class is currently using.
						echo "<input type='radio' name='template' checked='checked' value='$template_id'><font face='Verdana' size='2'>$template_name</font><br>";
					} else{
						echo "<input type='radio' name='template' value='$template_id'><font face='Verdana' size='2'>$template_name</font><br>";
					}
					insertTemplateId($template_id);
					$index++;
				endwhile ?>
			
			<?php
			$i=0;
			$incrementIdName=100;
			$incrementPercentName=200;
			$query_major = "SELECT * FROM major WHERE major.template_id='$templateused_id'";
			$result_major=mysql_query($query_major);
			$numMajor=mysql_num_rows($result_major);
			$query_currTemplateId="SELECT template_id FROM template WHERE template_name='$currTemplate'";
			?>
			
			<br /> 
				<?php if($numMajor > 0): ?>
					<p>The current grading rubric is consist of the following requirements:</p>
				<?php endif ?>
				<table border="0" class="pMirror">
				<?php
					//Display the current grading rubric. 
					while($i<$numMajor) {
						$major_name=mysql_result($result_major,$i,"name");
						$major_percent=mysql_result($result_major,$i,"percentage");
						$major_id=mysql_result($result_major,$i,"major_id");
						echo "<tr>";
							echo "<td><b>Major Requirement:</b></td>";
							echo "<td></td>";
							echo "<td> <input type='hidden' name='$i' value='$major_name'> $major_name </td>";
							echo "<td>$major_percent%</td>";
							echo "<td><input type='hidden' name='classid' value='$class_id'></td>";
						echo "</tr>";
						$query_minor = "SELECT minor.name, minor.maxrawscore, minor.name FROM major, minor WHERE major.major_id='$major_id'
						                AND major.major_id=minor.major_id AND major.template_id = '$templateused_id'
										ORDER BY minor.name";
						$result_minor=mysql_query($query_minor);
						$numMinor=mysql_numrows($result_minor);
						
						if ($numMinor>0){
							$ix=0;
							$incrementMinorId=300;
							$incrementMinorName=400;
							$incrementMaxRaw=500;
							echo "<tr>";
							echo "<td></td>";
							echo "<td></td>";
							echo "<td align='right'>Minor requirements:</td>";
							 while ($ix<$numMinor){
								$minor_name=mysql_result($result_minor,$ix,"name");
								$minor_maxraw=mysql_result($result_minor,$ix,"maxrawscore");
								echo "<td> <input type='hidden'></td>";
								echo "<td> <input type='hidden' name='$incrementMinorId' value='$minor_id'> </td>";
								echo "<td>$minor_name with $minor_maxraw items </td>";
								//echo "<td><input type='text' size='3'  name='$incrementMaxRaw' value='$minor_maxraw' class='noborder' />items</td>";
								$ix++;
								$incrementMinorId++;
								$incrementMinorName++;
								$incrementMaxRaw++;
							}
							echo "</tr>";
						}
					$i++;
					$incrementIdName++;
					$incrementPercentName++;
					}
				?>
				<?php if(mysql_num_rows($result_major) > 0): ?>
					<tr>
						<td></td>
						<td></td>
						<td><hr />For a total of:</td>
						<td><hr />100%</td>
					</tr>
				<?php endif ?>
				</table>
				<input type="submit" name="save" value="Save changes" /> 
				<input type="button" value="Edit template" onClick="editTemplate()" />
				<input type="button" value="Create new template" onClick="window.open('addTemplate.php', '_blank', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0')" />
				</form>
				
			</p>
			</div>
		<?php else: ?>
			<?php executeFile("../files/access_error.txt"); ?>
		<?php endif ?>
	</div>
	<div id="copywright"></div>
</body>
</html>